//DOM渲染完成
$(function () {
    //初始化编辑器
    var editUm = UM.getEditor('editReviewContent');
    editUm.ready(function () {
        editUm.execCommand('cleardoc');
    });
    //行业资讯
    var $editReviewName = $(".editReviewName"),
        $editReviewFloor = $(".editReviewFloor"),
        $editReviewTime = $(".editReviewTime"),
        $editReviewImg = $(".editReviewImg");
    //上传图片
    function upImgAjax(config) {
        EasyAjax.ajax_Upload_File({
                url: "file/slideUpload.json",
                data: config.fileData
            },
            function (data) {
                if(config.type){
                    config.obj.execCommand('inserthtml',"<img src='"+api_host+data.data+"' data-src='"+data.data+"' style='max-width: 100%;' alt='' />");
                }else {
                    config.obj.attr("src",api_host+data.data);
                    config.obj.attr("data-src",data.data);
                }
            });
    }
    //上传图片按钮
    $(".editUpImgFile").on("change",function(){
        var file=$(".editUpImgFile").get(0).files[0];
        var fileData=new FormData();
        fileData.append("upImg",file);
        $(".editUpImgFile").val("");
        upImgAjax({
            fileData:fileData,
            obj:$editReviewImg,
            type:0
        });
    });
    //编辑器上传图片按钮
    $(".editUmUpImgFile").on("change",function(){
        var file=$(".editUmUpImgFile").get(0).files[0];
        var fileData=new FormData();
        fileData.append("upImg",file);
        $(".editUmUpImgFile").val("");
        upImgAjax({
            fileData:fileData,
            obj:editUm,
            type:1
        });
    });
    //分页
    function page(data) {
        var nums = 10;
        var render = function (curr) {
            var last = curr * nums - 1,
                review = new Array();
            last = last >= data.data.length ? (data.data.length - 1) : last;
            for (var i = (curr * nums - nums); i <= last; i++) {
                review.push(data.data[i]);
            }
            var $review = $("#reviewTpl").render(review);
            return $review;
        };
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage({
                cont: 'page',
                pages: Math.ceil(data.data.length / nums),
                skip: true,
                jump: function (obj, first) {
                    $(".showReviewList").html(render(obj.curr));
                }
            });
        });
    }
    //初始化行业资讯
    function initReview() {
        EasyAjax.ajax_Get_Json({
                url: "review/getReviewList.json"
            },
            function (data) {
                page(data);
            });
    }
    initReview();
    //修改按钮
    var editReviewUid = "";
    $(document).on("click",".editReviewBtn",function () {
        var $this = $(this);
        editReviewUid = $this.parents("tr:first").attr("uid");
        EasyAjax.ajax_Get_Json({
                url: "review/queryReview.json",
                data: {editReviewUid:editReviewUid}
            },
            function (data) {
                var showReviewName = $this.parents("tr:first").find(".showReviewName").html(),
                    showReviewFloor = $this.parents("tr:first").find(".showReviewFloor").html(),
                    showReviewTime = $this.parents("tr:first").find(".showReviewTime").html(),
                    showReviewImg = data.data.operationTime,
                    showReviewContent = data.data.content;
                $editReviewName.val(showReviewName);
                $editReviewFloor.val(showReviewFloor);
                $editReviewTime.val(showReviewTime);
                $editReviewImg.attr('src',api_host+showReviewImg);
                $editReviewImg.attr('data-src',showReviewImg);
                editUm.setContent(showReviewContent);
                layer.open({
                    type: 1,
                    title: "编辑评论",
                    shadeClose: true,
                    padding:10,
                    area: ['980px', '500px'],
                    content: $('.editReviewTpl')
                });
            });
    });
    //添加按钮
    $(".addReviewBtn").on("click",function () {
        editReviewUid = "";
        $editReviewName.val("");
        $editReviewFloor.val("");
        $editReviewTime.val("");
        $editReviewImg.attr('src',"");
        $editReviewImg.attr('data-src',"");
        editUm.execCommand('cleardoc');
        layer.open({
            type: 1,
            title: "新增评论",
            shadeClose: true,
            padding:10,
            area: ['980px', '500px'],
            content: $('.editReviewTpl')
        });
    });
    //编辑行业资讯
    $(".savaEditReview").on("click",function () {
        var editReviewName = $editReviewName.val(),
            editReviewFloor = $editReviewFloor.val(),
            editReviewTime = $editReviewTime.val(),
            editReviewImg = $editReviewImg.attr("data-src"),
            editReviewContent = editUm.getContent(),
            $this = $(this);
        if(editReviewUid){
            //修改行业资讯
            var baseValue = {editReviewUid:editReviewUid,editReviewName:editReviewName,editReviewFloor:editReviewFloor,editReviewTime:editReviewTime,editReviewImg:editReviewImg,editReviewContent:editReviewContent};
            EasyAjax.ajax_Post_Json({
                    url: "review/updateReview.json",
                    data: {baseValue:JSON.stringify(baseValue)}
                },
                function (data) {
                    initReview();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("修改成功！");
                });
        }else{
            //添加行业资讯
            var baseValue = {setReviewName:editReviewName,setReviewFloor:editReviewFloor,setReviewTime:editReviewTime,setReviewImg:editReviewImg,setReviewContent:editReviewContent};
            EasyAjax.ajax_Post_Json({
                    url: "review/addReview.json",
                    data: {baseValue:JSON.stringify(baseValue)}
                },
                function (data) {
                    initReview();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("添加成功！");
                });
        }
    });
    //删除行业资讯
    $(document).on("click",".delReviewBtn",function () {
        editReviewUid = $(this).parents("tr:first").attr("uid");
        layer.confirm('确认删除', {
            btn: ['确认','取消'],
            anim: 6
        }, function(){
            EasyAjax.ajax_Post_Json({
                    url: "review/delReview.json",
                    data: {delReviewUid:editReviewUid}
                },
                function (data) {
                    initReview();
                    layer.msg("删除成功！");
                });
        }, function(){

        });
    });
});